package com.ytjj.qmyx.admin.service.imService.impl;


import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.qmyx.admin.mapper.ImIssueDescribeMapper;
import com.ytjj.qmyx.admin.model.ImIssueDescribe;
import com.ytjj.qmyx.admin.model.request.ImIssueDescribeReq;
import com.ytjj.qmyx.admin.model.response.im.ImIssueDescribeResponse;
import com.ytjj.qmyx.admin.model.vo.ImIssueDescribeVO;
import com.ytjj.qmyx.admin.model.vo.ImKeyDescribeVO;
import com.ytjj.qmyx.admin.service.imService.ImIssueDesscribeService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;

@Service
@AllArgsConstructor
public class ImIssueDescribeServiceImpl implements ImIssueDesscribeService {

    private ImIssueDescribeMapper imIssueDescribeMapper;

    @Override
    public CommonPage<ImIssueDescribeVO> describeList(ImIssueDescribeReq imIssueDescribeReq) {
        PageHelper.startPage(imIssueDescribeReq.getPageNum(), imIssueDescribeReq.getPageSize());
        List<ImIssueDescribeVO> pageList = imIssueDescribeMapper.describeList(imIssueDescribeReq);
        return CommonPage.restPage(pageList);
    }

    @Override
    public int insertDescribe(ImIssueDescribeResponse imIssueDescribeResponse) {
        ImIssueDescribe imIssueDescribe =new ImIssueDescribe();
        imIssueDescribe.setStandards(imIssueDescribeResponse.getStandards());
        imIssueDescribe.setAnswer(imIssueDescribeResponse.getAnswer());
        imIssueDescribe.setClassifyId(imIssueDescribeResponse.getClassifyId());
        imIssueDescribe.setCreateName(imIssueDescribeResponse.getCreateName());
        return imIssueDescribeMapper.insertDescribe(imIssueDescribe);
    }

    @Override
    public int updateDescribe(ImIssueDescribeResponse imIssueDescribeResponse) {
        ImIssueDescribe imIssueDescribe =new ImIssueDescribe();
        imIssueDescribe.setStandards(imIssueDescribeResponse.getStandards());
        imIssueDescribe.setId(imIssueDescribeResponse.getId());
        imIssueDescribe.setAnswer(imIssueDescribeResponse.getAnswer());
        imIssueDescribe.setClassifyId(imIssueDescribeResponse.getClassifyId());
        imIssueDescribe.setCreateName(UserUtil.getAdminId());
        return imIssueDescribeMapper.updateDescribe(imIssueDescribe);
    }

    @Override
    public int deleteDescribe(Integer id) {
        return imIssueDescribeMapper.deleteDescribe(id);
    }

    @Override
    public ImIssueDescribeResponse selectDescribe(Integer id) {
        ImIssueDescribeResponse imIssueDescribeResponse = new ImIssueDescribeResponse();
        ImIssueDescribeVO imIssueDescribeVO = imIssueDescribeMapper.selectDescribe(id);
        String[] split=imIssueDescribeVO.getSimilarity().split(",");
        imIssueDescribeResponse.setSimilarity(split);
        imIssueDescribeResponse.setAnswer(imIssueDescribeVO.getAnswer());
        imIssueDescribeResponse.setStandards(imIssueDescribeVO.getStandards());
        imIssueDescribeResponse.setStates(imIssueDescribeVO.getStates());
        return imIssueDescribeResponse;
    }

    @Override
    public int updateDescribeStates(ImIssueDescribe imIssueDescribe) {
        if (imIssueDescribe.getStates()==0){
            int count = imIssueDescribeMapper.findOpenCount();
            Assert.isTrue(count<10,"最大只能开启10个");
        }
        return imIssueDescribeMapper.updateDescribeStates(imIssueDescribe);
    }
}
